<?php

class Default_Model_Roles extends Zend_Db_Table_Abstract
{
    protected $_name = 'roles';
    protected $_primary = 'id';
    protected  $_order = 'roleName';
    
    public function getRolesUser($userId)
    {
        $query = $this->select();
        $query->setIntegrityCheck(false);
        $query->from(array('r'=>'roles'), array('r.roleName', 'r.id'));
        $query->join(array('ur'=>'users_roles'),'r.id = ur.roleId');
        $query->where('ur.userId = ?',$userId);
                                        
    	return $this->fetchAll($query);
    }
    public function addData($roleName)
    {
        $data = array('roleName' => $roleName);
        return $this->insert($data);
    }
    
    
    /*
    public function getAllData ()
    {
		$query = $this->select();
   		return $this->fetchAll($query);
    }
    //chua su dung


    public function getRoles ()
    {
		$select = $this->select();
   		return $this->fetchAll($select);
    }
    public function insertItem($info)
    {
        $this->insert($info);
        return $this->_identity;
    }

    public function updateItem($id,$info)
    {
        $this->update( $info, "id=" . (int) $id );
    }

    public function deleteItem($id)
    {
        $this->delete( "id=" . (int) $id );
    }

    public function getItems($option = array())
    {
        $count = isset($option['count'])? $option['count'] : 0;
        $offset = isset($option['offset'])? $option['offset'] : 0;
        $order = isset($option['order'])? $option['order'] : $this->_order;
        $sort = isset($option['sort'])? $option['sort'] : 'asc';
        $select = $this->select()->from(array('s' => "roles" ),
                        array(
                            'id',
                            'roleName'
                        ))
                ->order("{$order} {$sort}")
                ->limit($count,$offset);
        $results = $this->fetchAll($select);
        if ( !$results )
        {
            throw new Exception( 'Could not query : {}');
        }
        return $results;
    }

    public function getItem($id)
    {
        $result = array();
        $row = $this->fetchRow("id = {$id}");
        $result['id'] = $row->id;
        $result['roleName'] = $row->roleName;
        $privilegeModel = new Default_Model_Privilege();
        $result['resources'] = $privilegeModel->getResourcesByRoleId($id)->toArray();
        $result['projects'] = $privilegeModel->getProjectsByRoleId($id)->toArray();
        return $result;
    }

    public function countItems()
    {
        $query = $this->select()
                ->from('roles','count(*) as total');
        $result = $this->fetchRow($query);
        return $result->total;
    }
    */

}

























